//
//  SN_ForgetAndEditPwd_Controller.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/8/3.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_ForgetAndEditPwd_Controller.h"

@interface SN_ForgetAndEditPwd_Controller ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *topTextField;
@property (nonatomic, strong) UITextField *bottomTextField;

@property (nonatomic, strong) NSString *oriSzbPwd;
@property (nonatomic, strong) NSString *oriLoginPwd;

@end

@implementation SN_ForgetAndEditPwd_Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *navTitleStr = _type == 1 ? @"忘记密码" : @"修改密码";
    self.navBar.navigationItem.title = navTitleStr;
    
    UIBarButtonItem *confirmBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定 " style:UIBarButtonItemStyleDone target:self action:@selector(confirmAction:)];
    [confirmBtn setTintColor:[UIColor whiteColor]];
    self.navBar.navigationItem.rightBarButtonItem = confirmBtn;
    
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, 45)];
    topView.backgroundColor = [UIColor whiteColor];
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 140, 45)];
    leftLabel.font = [UIFont systemFontOfSize:15];
    leftLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    leftLabel.text = _type == 1 ? @"登录密码" : @"原解题密码";
    [topView addSubview:leftLabel];
    
    _topTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 0, VIEW_WIDTH - 140, 45)];
    _topTextField.placeholder = _type == 1 ? @"请输入" : @"输入四位数字";
    _topTextField.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    _topTextField.keyboardType = _type == 1 ? UIKeyboardTypeDefault : UIKeyboardTypeNumberPad;
    _topTextField.borderStyle = UITextBorderStyleNone;
    _topTextField.secureTextEntry = YES;
    _topTextField.font = [UIFont systemFontOfSize:15];
    [topView addSubview:_topTextField];
    [self.view addSubview:topView];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(topView.frame), VIEW_WIDTH - 15, 0.5)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
    [self.view addSubview:lineView];
    
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lineView.frame), VIEW_WIDTH, 45)];
    bottomView.backgroundColor = [UIColor whiteColor];
    UILabel *bottomLeftLabe = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 140, 45)];
    bottomLeftLabe.font = [UIFont systemFontOfSize:15];
    bottomLeftLabe.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    bottomLeftLabe.text = _type == 1 ? @"设置解题密码" : @"新解题密码";
    [bottomView addSubview:bottomLeftLabe];
    [self.view addSubview:bottomView];
    
    _bottomTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 0, VIEW_WIDTH - 140, 45)];
    _bottomTextField.delegate = self;
    _bottomTextField.placeholder =  @"输入四位数字";
    _bottomTextField.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    _bottomTextField.keyboardType = UIKeyboardTypeNumberPad;
    _bottomTextField.borderStyle = UITextBorderStyleNone;
    _bottomTextField.secureTextEntry = YES;
    _bottomTextField.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:_bottomTextField];
    
    
    _oriSzbPwd = [SYAccountTool ShareAccount].account.szbpwd;
    _oriLoginPwd = [SYAccountTool ShareAccount].account.loginpass;
}

// 确定
- (void)confirmAction:(UIButton *)sender {
    
    if (_bottomTextField.text.length != 4) {
        [Utils alertViewWithMessage:@"请输入四位解题密码"];
        return;
    }
    
    if (_type == 2) { //修改解题密码
        
        if (![_topTextField.text isEqualToString:_oriSzbPwd]) {
            [Utils alertViewWithMessage:@"密码输入错误,请重新输入"];
            return;
        }
        
        if (_bottomTextField.text.length != 4) {
            [Utils alertViewWithMessage:@"请输入4位数字"];
            return;
        }
        
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObject:@"api_user_updateszbpwd" forKey:@"method"];
        [param setObject:USERID forKey:@"userid"];
        [param setObject:@"1" forKey:@"type"];
        [param setObject:_topTextField.text forKey:@"oldszbpwd"];
        [param setObject:_bottomTextField.text forKey:@"szbpwd"];

        [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
            
            NSString *str = @"修改解题密码成功";
            [Utils alertViewWithMessage:str];
            [self.view endEditing:YES];
            
            NSMutableDictionary *re_dataDict = [[SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID] mutableCopy];
            
            [re_dataDict setObject:_bottomTextField.text forKey:@"szbpwd"];
            [SyUserInfomation personalImformationSaveWtih:re_dataDict];
            
//            [self.navigationController popViewControllerAnimated:YES];
            
            
        } failure:^(NSInteger recode, NSError *error) {
            
        }];

    }else if (_type == 1) { //忘记密码
    
        NSString *logintpwd = [Utils MD5EncryptWith:_topTextField.text];
        
        if (![logintpwd isEqualToString:_oriLoginPwd]) {
            
            [Utils alertViewWithMessage:@"登录密码输入错误,请重新输入"];
            return;
        }
        
        if (_bottomTextField.text.length != 4) {
            [Utils alertViewWithMessage:@"请输入4位数字"];
            return;
        }
        
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObject:@"api_user_resetszbpwd" forKey:@"method"];
        [param setObject:USERID forKey:@"userid"];
        
        [param setObject:logintpwd forKey:@"password"];
        [param setObject:_bottomTextField.text forKey:@"szbpwd"];
        
        [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
            
            NSString *str = @"修改解题密码成功";
            
            [self.view endEditing:YES];
            
            NSMutableDictionary *re_dataDict = [[SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID] mutableCopy];
            
            [re_dataDict setObject:_bottomTextField.text forKey:@"szbpwd"];
            [SyUserInfomation personalImformationSaveWtih:re_dataDict];
            
            if (self.confirmBlock) {
                self.confirmBlock(1);
            }
            
            [self.navigationController popViewControllerAnimated:YES];
            
            [self showHint:str];
            
        } failure:^(NSInteger recode, NSError *error) {
            
        }];

    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (string.length != 0 && textField.text.length >= 4) {
        return NO;
    }
    
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

@end
